home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-01-29 | 1.6 KB | 46 lines | [TEXT/IGR0] |
- | ReshapeMatrix
- | Allows you to redimension a 2-D matrix from NxM to KxL, as long as N*M == K*L
- | The Redimension command doesn't do this :-[
-
- | You can set newrows to NaN to have the macro choose the right number of rows
- | for the given value of newcols. The reverse is true, too.
- | However, they can't both be NaN!
-
- Macro ReshapeMatrix(matrix,newRows,newCols)
- String matrix
- Variable newRows=NaN,newCols=1 // defaults to 1 column
- Prompt matrix,"matrix to reshape",popup,WaveList("*",";","")
- Prompt newRows,"new number of rows, or NaN for auto"
- Prompt newCols,"new number of columns, or NaN for auto"
-
- Silent 1;PauseUpdate
- if( WaveDims($matrix) != 2 )
- Abort matrix+" is not a two-dimensional wave."
- endif
- Variable oldRows=DimSize($matrix,0)
- Variable oldCols=DimSize($matrix,1)
- Variable points= oldRows*oldCols
- Print "old number of rows= ",oldRows,", old number columns= ",oldCols
- if( (numtype(newRows) != 0) %& (numtype(newCols) != 0) )
- Abort "You must specify one or both of newrows and newcols"
- endif
- if( numtype(newRows) != 0 )
- newRows= points/newCols
- endif
- if( numtype(newCols) != 0 )
- newCols= points/newRows
- endif
- if( floor(newRows) != newRows )
- Abort "non-integer new rows: "+num2str(newRows)
- endif
- if( floor(newCols) != newCols )
- Abort "non-integer new columns: "+num2str(newCols)
- endif
- if( newRows * newCols != points )
- Abort "number of points mismatch: new= ",newRows * newCols," old=",points
- endif
- Redimension/N=(points) $matrix // redimension from matrix to vector
- Redimension/N=(newRows,newCols) $matrix // redimension from vector to matrix
- Print matrix+" reshaped to ",newRows," rows by ",newCols," columns"
- End
-